<?
    class Principio_Model_TypeMapper extends Principio_Library_Mapper
    {
        private static $db_table = null;

        private static function getDBTable()
        {
            if (self::$db_table == null)
                self::$db_table = new Principio_Model_DbTable_Type();

            return self::$db_table;
        }


        public static function delete($id, $use_transaction = true)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                Principio_Model_CompanyTypeExtTemplateMapper::deleteAllByType($id);
                Principio_Model_SpaMapper::deleteAllByType($id);
                Principio_Model_TypeExtTemplateMapper::delete($id);
                self::getDBTable()->delete(array("id=?" => $id));


                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                $return = self::reportException($ex);

                if ($use_transaction)
                {
                    self::getDBTable()->getAdapter()->rollback();
                    return false;
                }
                else
                    return $return;

            };
        }


        public static function save(Principio_Model_Type $model_type)
        {
            try
            {
                self::getDBTable()->getAdapter()->beginTransaction();

                $data = array(
                                "vendor" => "",
                                "firmware_id" => $model_type->getModelFirmware()->getID(),
                                "type" => $model_type->getType(),
                                "caption" => $model_type->getCaption(),
                                "exts" => $model_type->getExts()
                );

                if ($model_type->getID() == -1)
                {
                    $id = self::getDBTable()->insert($data);

                    $model_type_ext_template = new Principio_Model_TypeExtTemplate($model_type->getExts());
                    $model_type_ext_template->setTypeID($id);

                    Principio_Model_TypeExtTemplateMapper::insert($model_type_ext_template);
                    $model_type->setID($id);
                }
                else
                    self::getDBTable()->update($data, array("id=?" => $model_type->getID()));

                self::getDBTable()->getAdapter()->commit();

                return $model_type->getID();
            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollBack();

                return false;
            };
        }


        public static function get($id = null)
        {
            try
            {
                if (!is_null($id))
                {
                    $result = self::getDBTable()->find($id);
                    if ($result->count() != 1)
                        throw new Zend_Db_Exception("SPA Type with given ID doesn't exist");

                    return new Principio_Model_Type($result[0]->toArray());
                }
                else
                {
                    $values = self::getDBTable()->fetchAll();
                    $types = array();

                    foreach ($values as $type)
                        $types[] = new Principio_Model_Type($type->toArray());

                    return $types;
                };
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function checkUniqueType(Principio_Model_Type $model_type)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where("type=?", $model_type->getType())->where("id<>?", $model_type->getID())->columns("id");
                return (is_null(self::getDBTable()->fetchRow($select)));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function checkUniqueCaption(Principio_Model_Type $model_type)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where("caption=?", $model_type->getCaption())->where("id<>?", $model_type->getID())->columns("id");
                return (is_null(self::getDBTable()->fetchRow($select)));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }

        public static function search($string)
        {
            $search_fields = array(
                "type" => "Type",
                "caption" => "Caption",
                "firmware" => "Firmware"
            );

            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);

            foreach ($result as $res)
            {
                $model_search = new Principio_Model_Search($string, "Spa types");
                $model_search->setId($res["id"])->setResultName($res["caption"])->setOpenItemFunctionName("SpaTypes")->setParentID(false);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["id"]] = $model_search;
            }
            return $search;
        }


        /*
         * set firmware_id = -1
         */
        public static function resetFirmware($firmware_id)
        {
            self::getDBTable()->update(array("firmware_id" => "-1"), array("firmware_id=?" => $firmware_id));
        }


        public static function usedFirmware(Principio_Model_Firmware $model_firmware)
        {
            try
            {
                $select = self::getDBTable()->select(Zend_Db_Table_Abstract::SELECT_WITH_FROM_PART)->where("firmware_id=?", $model_firmware->getID())->columns(array("caption", "type"));
                $values = self::getDBTable()->fetchAll($select);
                $types = array();

                foreach ($values as $type_caption)
                    $types[] = array("caption" => $type_caption['caption'], "type" => $type_caption['type']);

                return $types;
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }
    }
?>